package com.example.administrator.printoutindemo;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;



import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.List;


public class StoreListAlarmOpAdapter extends BaseAdapter {
	private Context mContext = null;
	private List<StoreDoorAlarm> li = null;
	private LayoutInflater mInflater = null;

	private static final String WEBSERVICE_CONFIG = "webServiceInfo";
	private static final int LOGINSUCCESS = 0;
	private static final int NETWORKERROR = 2;
	private static final int SYSERROR = 3;
	private static final int UPSUCCESS = 4;
	private static final int UPEROOR = 5;

	ProgressDialog progressDiaog;

	private Handler handler;

	private class ViewHolder {
		TextView s_alarm_txt;
		Button store_alarm_off;
	}

	private ViewHolder holder = null;

	public StoreListAlarmOpAdapter(Context mContext, Handler handler) {
		this.mContext = mContext;
		this.handler = handler;

	}

	public void setList(List<StoreDoorAlarm> li) {
		this.li = li;
		this.mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return li.size();
	}

	@Override
	public Object getItem(int position) {
		return li.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (null == convertView) {
			holder = new ViewHolder();

			convertView = mInflater.inflate(R.layout.store_adapter_listalarm,
					null);

			holder.s_alarm_txt = (TextView) convertView
					.findViewById(R.id.s_alarm_txt);
			holder.store_alarm_off = (Button) convertView
					.findViewById(R.id.store_alarm_off);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (null != this.li) {

			StoreDoorAlarm o = li.get(position);

			if (o.getType() == 1) {
				holder.s_alarm_txt.setText("编号：" + o.getBh() + "\n地址："
						+ o.getParadd());
				holder.store_alarm_off.setText("关闭" + o.getBh() + "警报");

			}
			if (o.getType() == 2) {
				holder.s_alarm_txt.setText("编号：" + o.getBh() + "\n地址："
						+ o.getParadd());
				holder.store_alarm_off.setText("关闭" + o.getBh() + "仓门");

			}

			holder.store_alarm_off
					.setOnClickListener(new store_alarm_off_click(o));
		}

		return convertView;
	}

	private class store_alarm_off_click implements View.OnClickListener {

		private StoreDoorAlarm o;

		public store_alarm_off_click(StoreDoorAlarm o) {
			this.o = o;
		}

		@Override
		public void onClick(View v) {

			new Thread(new Runnable() {

				@Override
				public void run() {

					boolean bl = uploadInfo(o);

					if (bl) {
						Message m = new Message();
						m.what = StoreListAlarmOpAdapter.UPSUCCESS;
						handler.sendMessage(m);

					} else {
						Message m = new Message();
						m.what = StoreListAlarmOpAdapter.UPEROOR;
						handler.sendMessage(m);
					}
				}
			}).start();

		}
	}

	// 上传数据至服务器
	private boolean uploadInfo(StoreDoorAlarm s) {

		boolean bl = false;

		try {

			/****************** 调用webService ***********************/
			// 调用.net 的WebService.
			String nameSpace = "http://dyjapp.org/";
			String methodName = "addCommand";

			SharedPreferences sPreferencces = mContext.getSharedPreferences(
					WEBSERVICE_CONFIG, 0);

			String url = "http://" + sPreferencces.getString("ip", "") + ":"
					+ sPreferencces.getString("Port", "")
					+ "/StoreService.asmx";// "http://192.168.1.101/webservice/default.asmx";
			// // 10.0.2.2
			// 为Android模拟器的本地(localhost)IP
			String soapAction = nameSpace + methodName;

			// 设置连接参数
			SoapObject request = new SoapObject(nameSpace, methodName);

			// 增加属性参数。 将相关的函数参数放入到过程中。
			if (s.getType() == 1) {
				request.addProperty("type", "alarm");
				request.addProperty("paradd", s.getParadd());
			}
			if (s.getType() == 2) {
				request.addProperty("type", "close");
				request.addProperty("paradd", s.getParadd());
			}

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);// soap协议版本必须用SoapEnvelope.VER11（Soap
			// V1.1）;

			// 注意：这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice
			// 不指定rpc方式则用true否则要用false
			envelope.dotNet = true;

			// envelope.setOutputSoapObject(request);//设置请求参数
			envelope.bodyOut = request; // enveloper.bodyOut=request 与
			// envelope.setOutputSoapObject(request)
			// 效果相同。
			// step4 创建HttpTransportSE对象
			HttpTransportSE ht = new HttpTransportSE(url);
			// step5 调用WebService
			ht.call(soapAction, envelope); // 关键的一步，很多问题都在这一步调试时出现问题。要么是IIS有问题，要么是ksoap2相关参数没配置好。
			if (envelope.getResponse() != null) {
				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
				if ("ok".equals(response.toString())) {
					bl = true;
				}

			}
			/****************** 调用webService结束 ***********************/

		} catch (Exception e) {
			e.printStackTrace();
		}

		return bl;
	}
}